﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICAlOut : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        public string key = "";
        public string FBrNo = "";
        private string ID;

        public ICAlOut()
        {
            InitializeComponent();
        }

        public ICAlOut(string _ID)
        {
            InitializeComponent();
            this.ID = _ID;
        }

        private void ICAlOut_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }
        
        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetICAllocation("2");
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            //ql20181119
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{

                dataGrid1.DataSource = dt;
            //}
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICAllocation";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "调拨单号";
            cl2.MappingName = "FBrNo";
            cl2.Width = 120;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "开单时间";
            cl3.MappingName = "Fdate";
            cl3.Width = 80;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "开单人";
            cl4.MappingName = "FBillerID";
            cl4.Width = 50;
            dts.GridColumnStyles.Add(cl4);

            dataGrid1.TableStyles.Add(dts);

        }
        
        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FBrNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
        }
        //刷新
        private void bt_refresh_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }
        //返回
        private void bt_back_Click(object sender, EventArgs e)
        {
            Main main = new Main(this.ID);
            main.Show();
            this.Hide();
        }
        //明细
        private void bt_detail_Click(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            int num =0;
            ICAlOutDetail ica = new ICAlOutDetail(FBrNo,this.ID,out  num );
            if(num ==0)
            {
             MessageBox.Show("该单据的条码已经全部提交，请选择完成出库！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.Show();
                return;
            }
            else
            {
             ica.Show();
            this.Hide();
            }
           
        }
        //完成出库
        private void bt_finish_Click(object sender, EventArgs e)
        {
            if (FBrNo == null || FBrNo == "")
            {
                MessageBox.Show("请选择订单！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            DialogResult dr = MessageBox.Show("是否调拨出库单： " + FBrNo + " ？", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);

            if (dr == DialogResult.Yes)
            {
                //查询是否有未扫描的调拨单
                DataSet ddt = bjs.Selissmwc(FBrNo);
                DataTable dte = new DataTable();

                dte = ddt.Tables[0];
                if (dte.Rows.Count > 0)
                {
                    MessageBox.Show("有未扫描的调拨单，请扫描完毕之后在进行完成出库！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    bool lb = false;
                    lb = bjs.ExamineICA(FBrNo, Program.userid);
                    if (lb)
                    {
                        DataGridStyle();
                        BinData();
                    }
                    else
                    {
                        MessageBox.Show("调拨出库单失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                }

            }

        }

        

        
    }
}